package sort;

public class InsertSort {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        int[] arr = new int[80000];
        initArr(arr);
        insertSort(arr,1);
        //System.out.print(Arrays.toString(arr));
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");
    }

    static void initArr(int[] arr) {

        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 800000);
        }
    }
    public static void insertSort(int[] arr, int n) {
        int temp = 0;
        int insertIndex = 0;
        for (int i = n; i < arr.length; i++) {
            int j;
            for (j = i - 1; j >= 0; j--) {
                if (arr[i] > arr[j]) {
                    temp = arr[i];
                    insertIndex = j + 1;
                    break;
                }
            }
            if (j < 0) {
                temp = arr[i];
                insertIndex = j + 1;
            }
            for (int k = i; k > insertIndex; k--) {
                arr[k] = arr[k - 1];
            }
            arr[insertIndex] = temp;
        }
    }
}
